// pages/shoppingcart/shoppingcart.js
const app = getApp();
const api = require("../../utils/api.js").default;
const myRequest = require("../../utils/request.js").default;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total: 0,
    selectall:0,
    cart:[],
    count:0,
    num:0,
    minusStatuses:[],
    haslist:false,
    cartTotal : []
  },
  // 总价格
  // gettotal(){
  //   let carts = this.data.cartList;
  //   let total = 0;
  //   let count = 0;
  //   for (let i = 0; i < carts.length; i++) {
  //     let selected = carts[i].checked;
  //     if (selected) {
  //       total += carts[i].number * carts[i].retail_price;
  //     }
  //   }
  //   console.log("gdgdgdg",total)
  //   this.setData({
  //     cart:carts,
  //     total:total
  //   })
  // },
  // 单选
  checkboxChange: function (e) {
    var item = e.currentTarget.dataset.item;
    var that = this;
    const token = wx.getStorageSync("token");
    var check = item.checked?0:1;
    wx.request({
      url: api.checked,
      method: "POST",
      data:{
        productIds: item.product_id,
        isChecked: check
      },
      header: {
        'content-type': 'application/json',
        'X-nideshop-token': token
      },
      success(res){
        that.isselect(res.data.data.cartList);
        that.setData({
          cartList: res.data.data.cartList,
          cartTotal: res.data.data.cartTotal,
          selectall: that.data.selectall
        })
      }
    })
  },
  // 全选
  selectall(e){
    let id = [];
    var that=this;  
    this.data.cartList.forEach((e)=>{
      id.push(e.product_id)
    })
    id = id.join();
    const token = wx.getStorageSync("token");  
    wx.request({
      url: api.checked,
      method: "POST",
      data: {
        productIds: id,
        isChecked: this.data.selectall ? 0 : 1
      },
      header: {
        'content-type': 'application/json',
        'X-nideshop-token': token
      },
      success(res) {
        that.isselect(res.data.data.cartList);
        that.setData({
          cartList: res.data.data.cartList,
          cartTotal: res.data.data.cartTotal
        })
      }
    })
  },
  isselect(e){
    var k=1;
    e.forEach((e) => {
      if(!e.checked){
        k=0;
      }
    })
    this.setData({
      selectall: k
    })
  },
  sendmessge(e, num) {
    var that = this;
    let index = e.currentTarget.dataset.index;
    var item = e.currentTarget.dataset.item;
    const token = wx.getStorageSync("token");
    let minusStatus = num <= 1 ? 'disabled' : 'normal';
    let minusStatuses = this.data.minusStatuses;
    minusStatuses[index] = minusStatus;
    wx.request({
      url: api.updata,
      method: "POST",
      data: {
        number: num,
        id: item.id,
        goodsId: item.goods_id,
        productId: item.product_id
      },
      header: {
        'content-type': 'application/json',
        'X-nideshop-token': token
      },
      success(res) {
        that.setData({
          cartList: res.data.data.cartList,
          minusStatuses: minusStatuses,
          cartTotal: res.data.data.cartTotal
        })
      }
    })
  },
  // 添加
  addcount(e){
    var item = e.currentTarget.dataset.item;
    var num = item.number;
    num++; 
    this.sendmessge(e, num);
  },
  // 减少
  reducecount(e) {
    var item = e.currentTarget.dataset.item;
    var num = item.number;
    if (num > 1) {
      num--;
    }
    this.sendmessge(e, num);
  },
  bindkeyinput(e) {
    var num = e.detail.value;
    if (num <= 1) {
      num = 1;
    }
    this.sendmessge(e, num);
  },
  // 删除
  deletelist(e) {
    var index = e.currentTarget.dataset.index;
    var item = e.currentTarget.dataset.item;
    const token = wx.getStorageSync("token");
    var that = this;
    var id = item.product_id;
    var pid = id.toString();
    this.data.cartList.splice(index, 1);
    if(index < 1){
      that.setData({
        haslist: false
      })
    }
    wx.request({
      url: api.delete,
      method: "POST",
      data: {
        productIds: pid
      },
      header: {
        'content-type': 'application/json',
        'X-nideshop-token': token
      },
      success(res) {
        that.setData({
          cartList: res.data.data.cartList,
          cartTotal: res.data.data.cartTotal
        })
      }
    })
    wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 2000
    })
  },
  //打开付款提示
  setPayment: function (e) {
    var item = e.currentTarget.dataset.item;
    var that = this;
    var n = 0;
    item.forEach(function(i){
      if(i.checked == 1){
        that.setData({
          isRuleTrue: true
        })
      }
      if (i.checked == 0){
        n++;
      }
    })
    if(n == item.length){
      wx.showToast({
        title:"请选择商品",
        icon:"none",
        duration: 1000
      })
    }
  },
  //关闭付款提示
  hideRule: function () {
    this.setData({
      isRuleTrue: false
    })
  },
  submit(){
    wx.showToast({
      title: "支付成功",
      icon: "success",
      duration: 1000
    })
    this.setData({
      isRuleTrue: false
    })
  },
  tocomdetail(e) {
    console.log(e.currentTarget.dataset);
    var goodsid = e.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: '/pages/commodities/comdetail/comdetail?goodsid=' + goodsid
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (this.data.haslist == false){
      this.setData({
        textcart:"购物车空了，快去添加商品吧！！",
        selectall: 0
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const token = wx.getStorageSync("token");
    var self = this;
    wx.request({
      url: api.cart,
      method: "GET",
      header: {
        'content-type': 'application/json',
        'X-nideshop-token': token
      },
      success(res) {
        self.isselect(res.data.data.cartList);
        self.setData({
          haslist: true,
          cartList: res.data.data.cartList,
          cartTotal: res.data.data.cartTotal
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})